<?php
/**
 * @author  lvzhendong
 */
namespace Backend\Model;
use Think\Model;

class MediaModel extends Model
{
    public function __construct() {
        parent::__construct();
    }

    protected $_validate = [
        //array(验证字段1,验证规则,错误提示,[验证条件,附加规则,验证时间])
        array('name', 'require', '请填写媒体名称！'),
        array('platform', 'require', '请选择媒体平台！', '', '', 3),
        array('domain', 'require', '请填写媒体地址！'),
        array('domain', 'url', '请填写合法的媒体地址！'),
        //array('domain', '', '此媒体地址已存在！', 0, 'unique', 3),
        array('contact', 'require', '请填写媒体联系人！'),
    ];

    protected function _before_insert(&$data) {
        $data['appsecret']  =   $this->make_appsecret();
        $data['appid']      =   $this->make_appid();
    }

    /**
     * 获取媒体信息
     * @param array $where
     * @return mixed
     */
    public function get_media_info($where = []) {
        if(!empty($where) && is_array($where))
            return $this->where($where)->order('id DESC')->select();
        else
            return $this->order('id DESC')->select();
    }

    /**
     * 格式化媒体数据[媒体平台]
     * @param $data
     * @return array
     */
    public function get_format_media($data) {
        if(!empty($data) && is_array($data)) {
            $platform = $data['platform'];
            if(strstr($platform, ',')) {
                $platform = ['platform' => explode(',', $platform ) ];

                return array_merge($data, $this->array_rev($platform['platform']));
            } else {
                return array_merge($data, $this->array_rev([$data['platform']]));
            }
        } else {
            return $data;
        }

    }

    /**
     * 辅助方法['android', 'ios', 'h5'] -> ['android'=>'android', 'ios' => 'ios', 'h5' => 'h5']
     * @param $data
     * @return array
     */
    public function array_rev($data) {
        $arrayRev = [];
        $range = ['android', 'ios', 'h5'];

        foreach ($data as $k =>$v) {
            if (in_array($v, $range)) {
                $arrayRev[$v] .= $v;
            }
        }

        return ['platform' => $arrayRev];
    }

    /**
     * 生成appid
     * @return int
     */
    public function make_appid() {
        $rule['user_id']    =   '110';
        return time();
    }

    /**
     * 生成appsecret
     * @return string
     */
    public function make_appsecret() {
        return md5(time());
    }

    /**
     * 生成随机字符串
     * @param $rLength              数据打乱次数，默认为 3
     * @param string $execNum       返回数据长度
     * @return bool|string
     */
    public function make_rand_str($rLength, $execNum = '3') {
        $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
        $string = time();
        for(;$execNum >= 1; $execNum--) {
            $position_1 = mt_rand()%strlen($chars);
            $position_2 = mt_rand()%strlen($string);
            $string = substr_replace($string, substr($chars, $position_1, 1), $position_2, 0);
        }
        return substr($string, 0, $rLength);
    }

}